package com.eflow.model.flowable.converter;

import com.eflow.model.domain.EflowUser;
import com.eflow.model.expetion.FlowableException;
import com.eflow.model.flowable.handler.IProxyUserHandler;
import com.eflow.model.flowable.model.UserElement;
import com.eflow.model.flowable.model.e.UserTypeEnum;
import org.flowable.engine.repository.ProcessDefinition;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * @author hui se
 * @description
 * @create 2021-03-05 18:12
 **/
public abstract class BaseUserListConverter {
    @Autowired
    private ObjectProvider<IProxyUserHandler> processListeners;

    abstract List<EflowUser> list(UserElement userElement, String loginUser, ProcessDefinition processDefinition, String processInstanceId, String activityId, Map<String,Object> var);

    abstract UserTypeEnum getUserType();

    public List<EflowUser> porxyList(UserElement userElement, String loginUser, ProcessDefinition processDefinition, String processInstanceId, String activityId, Map<String,Object> var){
        List<EflowUser> userList = list(userElement, loginUser, processDefinition, processInstanceId, activityId, var);
        Iterator<IProxyUserHandler> iterator = processListeners.iterator();
        while(iterator.hasNext()) {
            IProxyUserHandler next = iterator.next();
            userList =  next.proxyList(userList,userElement, loginUser, processDefinition, processInstanceId, activityId, var);
            if(userList == null){
                throw new FlowableException("审批用户不能为null");
            }
        }
        return userList;
    }
}
